package com.syygl.test.study.syyCode;

import java.util.LinkedList;
import java.util.Queue;

public class TreeMethod {
    //打印树的节点值
    public static void printTreeNodeVal(TreeNode root){
        if(root == null) return;
        //这里打印是前序
        System.out.println(root.val);
        printTreeNodeVal(root.left);
        //这里打印是中序
        printTreeNodeVal(root.right);
        //这里打印是后序
    }

    //按层级打印树
    public static void printTreeNodeByLevel(TreeNode root){
        if(root == null) return;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode poll = queue.poll();
            if(poll != null){
                System.out.println(poll.val);
                queue.offer(poll.left);
                queue.offer(poll.right);
            }
        }
    }


    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode left1 = new TreeNode(2);
        TreeNode right1 = new TreeNode(3);
        TreeNode left11 = new TreeNode(4);
        TreeNode right11 = new TreeNode(5);
        TreeNode left22 = new TreeNode(6);
        TreeNode right22 = new TreeNode(7);

        root.left = left1;
        root.right = right1;
        left1.left = left11;
        left1.right = right11;
        right1.left  = left22;
        right1.right = right22;

        printTreeNodeVal(root);
        System.out.println(">>>>>>>>>>>>>>>>");
        printTreeNodeByLevel(root);


    }

}
